1
/****************************** Module Header ******************************\
2 * Module Name: MainForm.cs
3 * Project: CSWinFormSingleInstanceApp
4 * Copyright (c) Microsoft Corporation.
6 * The sample demonstrates how to achieve the goal that only
7 * one instance of the application is allowed in Windows Forms application..
9 * This source is subject to the Microsoft Public License.
10 * See http://www.microsoft.com/opensource/licenses.mspx#Ms-PL.
11 * All other rights reserved.
14 * * 8/31/2009 3:00 PM Bruce Zhou Created
15 \***************************************************************************/
18 using System
.Collections
.Generic
;
19 using System
.ComponentModel
;
24 using System
.Windows
.Forms
;
26 namespace CSWinFormSingleInstanceApp
28 public partial class MainForm
: Form
31 LoginForm loginEntry
= new LoginForm();
33 event EventHandler LoginMsgChanged
;
37 public string LoginMsg
39 get { return loginMsg; }
42 if (value != loginMsg
)
45 OnLoginMsgChanged(EventArgs
.Empty
);
54 InitializeComponent();
55 this.Load
+= new EventHandler(MainForm_Load
);
56 this.StartPosition
= FormStartPosition
.CenterScreen
;
62 void MainForm_Load(object sender
, EventArgs e
)
65 loginEntry
.ShowDialog();
66 //display welcome message
67 DisplayWelcomeMessage();
68 //bind Text of lableUserStatus to LoginMsg property.
69 this.labelUserStatus
.DataBindings
.Add("Text", this, "LoginMsg");
71 private void buttonLogoff_Click(object sender
, EventArgs e
)
73 GlobleData
.IsUserLoggedIn
= false;
74 LoginMsg
= "User Successfully logged off";
77 loginEntry
= new LoginForm();
78 if (loginEntry
.ShowDialog() == DialogResult
.OK
)
80 if (this.Visible
== false && GlobleData
.IsUserLoggedIn
)
84 LoginMsg
= "User Successfully logged in";
90 void DisplayWelcomeMessage()
92 string welcomeMsg
= "Welcome to codeplex:" + GlobleData
.UserName
;
93 this.labelWelcomeMsg
.Text
= welcomeMsg
;
94 LoginMsg
="User Successfully logged in";
98 void OnLoginMsgChanged(EventArgs e
)
100 if (LoginMsgChanged
!= null)
102 LoginMsgChanged(this, e
);